﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using SmartWeb.BLL;
using Yipusoft.Account.Model;

public partial class DutyLog_ParkLogView : System.Web.UI.Page
{
    private string DepartmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["DepartmentID"] != null)
        {
            DepartmentID = Request["DepartmentID"];
        }
        if (Request["Date"] == null)
            return;

        if (!IsPostBack)
            Bind();
    }
    private void Bind()
    {
        List<ParkType> listType = Park.GetList();
        for (int n = 1; n < 6; n++)
        {
            string id = "lbl" + n.ToString();
            Label lbl = pn.FindControl(id) as Label;
            if (lbl != null && listType[n - 1] != null)
            {
                lbl.Text = listType[n - 1].ItemName;
            }
        }
        int cuDay = 0;
        if (DateTime.Parse(Request["Date"]).DayOfWeek == 0)
        {
            cuDay = 7;
        }
        else
        {

            cuDay = Convert.ToInt32(DateTime.Parse(Request["Date"]).DayOfWeek);
        }

        int temp = (-1) * cuDay;
        DateTime StartTime = DateTime.Parse(DateTime.Parse(Request["Date"]).AddDays(temp + 1).ToString("yyyy-MM-dd"));
        DateTime EndTime = DateTime.Parse(StartTime.AddDays(7).ToString("yyyy-MM-dd"));
        ParkLogs park = Park.GetParkLog(StartTime, EndTime, DepartmentID);
        if (park == null)
            return;

        txtCaption.Text = park.Captain;
        txtDepartment.Text = park.DepartmentName;
        txtDate.Text = park.Date.Value.ToString("yyyy-MM-dd");

        List<ParkContent> listContent = Park.GetParkContent(StartTime, EndTime, DepartmentID);

        for (int i = 1; i < 6; i++)
        {
            string id = "lblCar" + i.ToString();
            Label lblCar = pn.FindControl(id) as Label;
            if (lblCar != null && listContent[i - 1] != null)
            {
                lblCar.Text = UserFun.HtmlToText(listContent[i - 1].Contents);
            }
        }

        string Departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;

        DateTime CurrentStartDate = new DateTime();
        if (Convert.ToInt32(DateTime.Now.DayOfWeek) == 0)
        {
            CurrentStartDate = Convert.ToDateTime(DateTime.Now.AddDays(-7).ToShortDateString());
        }
        else
        {
            CurrentStartDate = Convert.ToDateTime(DateTime.Now.AddDays(-1 * Convert.ToInt32(DateTime.Now.DayOfWeek)).ToShortDateString());
        }

        if (DateTime.Now.DayOfWeek == DayOfWeek.Friday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Monday)
        {
            if (park.Date.Value == CurrentStartDate.AddDays(5) || park.Date.Value == CurrentStartDate.AddDays(6) || park.Date.Value == CurrentStartDate.AddDays(7) || park.Date.Value == CurrentStartDate.AddDays(-7))
            {
                if ((park.Captain == null || park.Captain.Trim() == "") && isOk(Departmentid, park))
                {
                    Button1.Visible = true;
                }
                else
                {
                    Button1.Visible = false;
                }
            }
            else
            {
                Button1.Visible = false;
            }
        }
        else
        {
            Button1.Visible = false;
        }

    }

    bool isOk(string departmentid, ParkLogs park)
    {
        if (park.DepartmentID.Trim() == departmentid.Trim())
        {
            return true;
        }
        else
        {
            if (departmentid.Substring(4, 1) == "2")
            {
                string ShortName = Yipusoft.Account.BLL.Department.GetDepartment(departmentid).ShortName.Substring(0, 2);
                List<DepartmentInfo> departments = Yipusoft.Account.BLL.Department.GetList(" departmentid like '" + departmentid.Substring(0, 4) + "3__' and ShortName like '" + ShortName + "%'  ").ToList();
                if (departments.Count > 0)
                {
                    bool ok = false;
                    for (int i = 0; i < departments.Count; i++)
                    {
                        if (departments[i].DepartmentID.Trim() == park.DepartmentID.Trim())
                        {
                            ok = true;
                            break;
                        }
                    }
                    return ok;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        //string userID = BLL.User.GetUserByUserName(User.Identity.Name).UserID;
        //ParkLogs park = new ParkLogs();
        //park.ID = Guid.NewGuid();
        //park.SetDate =DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
        //park.DepartmentID = DepartmentID;
        //park.SetUsers = userID;
        //park.DepartmentName = txtDepartment.Text;
        //park.Captain = txtCaption.Text;
        //park.Date = DateTime.Parse(txtDate.Text);

        //List<ParkContent> listPark = new List<ParkContent>();

        //for (int i = 1; i < 6; i++)
        //{
        //    string id = "txtCar" + i.ToString();
        //    TextBox txtCar = pn.FindControl(id ) as TextBox;
        //    if (txtCar != null)
        //    {
        //        ParkContent pkct = new ParkContent();
        //        pkct.ID = Guid.NewGuid();
        //        pkct.PID = park.ID;
        //        pkct.Contents = UserFun.TextToHtml(txtCar.Text);
        //        pkct.Tid = i;
        //        listPark.Add(pkct);
        //    }
        //}

        // int cuDay =Convert.ToInt32(DateTime.Now.DayOfWeek);
        //int temp = (-1) * cuDay;
        //DateTime StartTime = DateTime.Parse(DateTime.Now.AddDays(temp).ToString("yyyy-MM-dd"));
        //DateTime EndTime = DateTime.Parse(StartTime.AddDays(7).ToString("yyyy-MM-dd"));
        //Park.DeleteParkLog(StartTime, EndTime, DepartmentID);
        //Park.Insert(listPark);
        //Park.Insert(park);


    }

    //签字
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<ParkType> listType = Park.GetList();
        for (int n = 1; n < 6; n++)
        {
            string id = "lbl" + n.ToString();
            Label lbl = pn.FindControl(id) as Label;
            if (lbl != null && listType[n - 1] != null)
            {
                lbl.Text = listType[n - 1].ItemName;
            }
        }
        int cuDay = Convert.ToInt32(DateTime.Parse(Request["Date"]).DayOfWeek);
        int temp = (-1) * cuDay + 1;
        DateTime StartTime = DateTime.Parse(DateTime.Parse(Request["Date"]).AddDays(temp).ToString("yyyy-MM-dd"));
        DateTime EndTime = DateTime.Parse(StartTime.AddDays(7).ToString("yyyy-MM-dd"));
        ParkLogs park = Park.GetParkLog(StartTime, EndTime, DepartmentID);
        Yipusoft.SecurityEvaluate.ParkLogDataContext db = new ParkLogDataContext();
        park = db.ParkLogs.Where(o => o.ID == park.ID).FirstOrDefault();
        park.Captain = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
        db.SubmitChanges();
        Bind();
    }
}
